﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using GameObjects;
using GameObjects.DrawableClasses;
using GameObjects.MoveBehaviors;
using Microsoft.Xna.Framework;
using Virvar.Net;
using GameProcess;

namespace GameServer
{
    public class GameLogic
    {
        public static GameLogic Instance;
        Stopwatch stopwatch;
        TimeSpan lastUpdTime;
        int updateInterval = 40;
        bool shouldContinue;

        public static void CreateInstance()
        {
            Instance = new GameLogic();
        }

        private GameLogic()
        {
            Initialize();
        }

        public void Initialize()
        {
            //// монстр
            //{
            //    RealCharacter monster = GameState.Instance.CreateCharacter(false);
            //    //monster.moveBehavior = new StupidMonsterMove(monster);
            //    monster.MoveBehavior = new EmptyBehavior(monster);
            //    monster.Position = new Vector2(150, 200);
            //    monster.Speed = 25;
            //}
            //// второй монстр
            //{
            //    RealCharacter monster = GameState.Instance.CreateCharacter(false);
            //    //monster.moveBehavior = new StupidMonsterMove(monster);
            //    monster.MoveBehavior = new EmptyBehavior(monster);
            //    monster.Position = new Vector2(200, 170);
            //    monster.Speed = 25;
            //}
            shouldContinue = true;
            Start();
        }

        void Start()
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();
            lastUpdTime = TimeSpan.Zero;
            new Thread(Update).Start();
        }

        public void Update()
        {
            while (shouldContinue)
            {
                GameTime gameTime = new GameTime(stopwatch.Elapsed, stopwatch.Elapsed - lastUpdTime);
                lastUpdTime = stopwatch.Elapsed;
                GameState.Instance.Update(gameTime);
                Thread.Sleep(updateInterval);
            }
        }

        public void Stop()
        {
            shouldContinue = false;
        }

        ~GameLogic()
        {
            shouldContinue = false;
            stopwatch.Stop();
        }
    }
}
